import {
    getAddressList,
    delAddress,
    editAddress
} from '../../api/user.js';
import {
    baseConfig
} from '../../api/api.js';
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        parameter: {
            'navbar': '1',
            'return': '1',
            'title': '身份信息'
        },
        addressList: [],
        cartId: '',
        pinkId: 0,
        couponId: 0,
        loading: false,
        loadend: false,
        loadTitle: '加载更多',
        page: 1,
        limit: 8,
        select: false,
        select_type: 1,
        add: false,
        seletList: [],
        hideId: '',
        product_num: 0,
        order_information_limit: 1, // 每个地址可购买数量
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            cartId: options.cartId || '',
            pinkId: options.pinkId || 0,
            couponId: options.couponId || 0,
            select: options.select || false,
            select_type: options.select_type || 1,
            add: options.add || false,
            hideId: options.hideId || '',
            product_num: options.allNum || 0, //专辑总数
            'parameter.title': options.address ? '地址信息' : '身份信息'
        })
    },
    onShow: function () {
        var page = getCurrentPages();
        var prePage = page[page.length - 2]; //上个页面
        this.setData({
            product_num: prePage.data.allNum
        })
        var that = this;
        if (app.globalData.isLog) that.getAddressList(true);
        this.getBaseConfig()
    },
    onLoadFun: function () {
        this.getAddressList();
    },
    getBaseConfig() {
        let that = this
        baseConfig().then(res => {
            that.setData({
                order_information_limit: res.data.order_information_limit
            })
        })
    },
    /**
     * 获取地址列表
     * 
     */
    getAddressList: function (isPage) {
        var that = this;
        if (isPage) that.setData({
            loadend: false,
            page: 1,
            addressList: []
        });
        if (that.data.loading) return;
        if (that.data.loadend) return;
        that.setData({
            loading: true,
            loadTitle: ''
        });
        getAddressList({
            page: that.data.page,
            limit: that.data.limit
        }).then(res => {
            var list = res.data;
            var loadend = list.length < that.data.limit;
            that.data.addressList = app.SplitArray(list, that.data.addressList);
            that.setData({
                addressList: that.data.addressList,
                loadend: loadend,
                loadTitle: loadend ? '已全部加载' : '加载更多',
                page: that.data.page + 1,
                loading: false,
            });
        }).catch(err => {
            that.setData({
                loading: false,
                loadTitle: '加载更多'
            });
        });
    },
    /**
     * 编辑地址
     */
    editAddress: function (e) {
        var cartId = this.data.cartId,
            pinkId = this.data.pinkId,
            couponId = this.data.couponId;
        this.setData({
            cartId: '',
            pinkId: '',
            couponId: ''
        })
        wx.navigateTo({
            url: '/pages/user_address/index?id=' + e.currentTarget.dataset.id + '&cartId=' + cartId + '&pinkId=' + pinkId + '&couponId=' + couponId
        })
    },
    /**
     * 删除地址
     */
    delAddress: function (e) {
        var index = e.currentTarget.dataset.index,
            that = this,
            address = this.data.addressList[index];
        if (address == undefined) return app.Tips({
            title: '您删除的地址不存在!'
        });
        delAddress(address.id).then(res => {
            app.Tips({
                title: '删除成功',
                icon: 'success'
            }, function () {
                that.data.addressList.splice(index, 1);
                that.setData({
                    addressList: that.data.addressList
                });
            });
        }).catch(err => {
            return app.Tips({
                title: err
            });
        });
    },
    /**
     * 新增地址
     */
    addAddress: function () {
        var cartId = this.data.cartId,
            pinkId = this.data.pinkId,
            couponId = this.data.couponId;
        this.setData({
            cartId: '',
            pinkId: '',
            couponId: ''
        })
        wx.navigateTo({
            url: '/pages/user_address/index?cartId=' + cartId + '&pinkId=' + pinkId + '&couponId=' + couponId
        })
    },
    selectAddress(e) {
        var page = getCurrentPages();
        var prePage = page[page.length - 2]; //上个页面

        let {address} = e.detail.value
        let {
            select_type,
            product_num,
            order_information_limit,
            addressList
        } = this.data

        let list = []
        let address_ids = []
        if (select_type == 2) {
            address_ids.push(...address)
        } else {
            address_ids.push(address)
        }

        addressList.forEach(item => {
            if (address_ids.indexOf(String(item.id)) > -1) {
                list.push(item)
            }
        })

        let address_num = Math.ceil(Number(product_num) / order_information_limit); //所需地址数量

        if (list.length == address_num) {
            prePage.setData({
                address: list,
            },()=>{
                prePage.computedPrice();
                wx.navigateBack({
                    delta: 0,
                })
            })
        } else {
            return getApp().Tips({
                title: `根据专辑数量，您必须选择${address_num}个地址信息`
            })
        }
    },
    /**
     * 
     * @param {添加收货地址} e 
     */
    addAAddress(e) {
        let item = e.currentTarget.dataset.item
        if (this.data.add && item) {
            item.is_default = 1
            var page = getCurrentPages();
            var prePage = page[page.length - 2]; //上个页面
            prePage.setData({
                addressInfo: item,
                seletAddressList: [item]
            }) //修改上个页面的data
            prePage.computedPrice();
            wx.navigateBack({
                delta: 0,
            })
        }
    },
    goOrder: function (e) {
        var id = e.currentTarget.dataset.id;
        var cartId = '';
        var pinkId = '';
        var couponId = '';
        if (this.data.cartId && id) {
            cartId = this.data.cartId;
            pinkId = this.data.pinkId;
            couponId = this.data.couponId;
            this.setData({
                cartId: '',
                pinkId: '',
                couponId: '',
            })
            wx.redirectTo({
                url: '/pages/order_confirm/index?is_address=1&cartId=' + cartId + '&addressId=' + id + '&pinkId=' + pinkId + '&couponId=' + couponId
            })
        }
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.getAddressList();
    }
})
